// 权限管理工具
class PermissionManager {
  // 检查位置权限
  static checkLocationPermission() {
    return new Promise((resolve, reject) => {
      wx.getSetting({
        success: (res) => {
          if (res.authSetting["scope.userLocation"] === true) {
            resolve(true);
          } else if (res.authSetting["scope.userLocation"] === false) {
            resolve(false);
          } else {
            // 未设置过权限
            resolve(null);
          }
        },
        fail: reject,
      });
    });
  }

  // 请求位置权限
  static requestLocationPermission() {
    return new Promise((resolve, reject) => {
      wx.authorize({
        scope: "scope.userLocation",
        success: resolve,
        fail: reject,
      });
    });
  }

  // 打开设置页面
  static openSetting() {
    return new Promise((resolve, reject) => {
      wx.openSetting({
        success: resolve,
        fail: reject,
      });
    });
  }

  // 显示权限说明弹窗
  static showPermissionModal(title, content) {
    return new Promise((resolve) => {
      wx.showModal({
        title: title || "需要权限",
        content: content || "为了更好的体验，需要获取相关权限",
        confirmText: "去设置",
        cancelText: "取消",
        success: (res) => {
          resolve(res.confirm);
        },
      });
    });
  }

  // 完整的位置权限流程
  static async handleLocationPermission() {
    try {
      const permissionStatus = await this.checkLocationPermission();

      if (permissionStatus === true) {
        return true; // 已有权限
      } else if (permissionStatus === false) {
        // 用户拒绝了权限，引导去设置
        const confirmed = await this.showPermissionModal(
          "需要位置权限",
          "为了更好的体验，需要获取您的位置信息"
        );

        if (confirmed) {
          const settingRes = await this.openSetting();
          return settingRes.authSetting["scope.userLocation"] || false;
        }
        return false;
      } else {
        // 首次请求权限
        try {
          await this.requestLocationPermission();
          return true;
        } catch (err) {
          console.log("位置权限请求失败:", err);
          return false;
        }
      }
    } catch (err) {
      console.log("权限检查失败:", err);
      return false;
    }
  }

  // 检查相机权限
  static checkCameraPermission() {
    return new Promise((resolve, reject) => {
      wx.getSetting({
        success: (res) => {
          if (res.authSetting["scope.camera"] === true) {
            resolve(true);
          } else if (res.authSetting["scope.camera"] === false) {
            resolve(false);
          } else {
            resolve(null);
          }
        },
        fail: reject,
      });
    });
  }

  // 请求相机权限
  static requestCameraPermission() {
    return new Promise((resolve, reject) => {
      wx.authorize({
        scope: "scope.camera",
        success: resolve,
        fail: reject,
      });
    });
  }

  // 完整的相机权限流程
  static async handleCameraPermission() {
    try {
      const permissionStatus = await this.checkCameraPermission();

      if (permissionStatus === true) {
        return true;
      } else if (permissionStatus === false) {
        const confirmed = await this.showPermissionModal(
          "需要相机权限",
          "为了拍摄照片，需要获取相机权限"
        );

        if (confirmed) {
          const settingRes = await this.openSetting();
          return settingRes.authSetting["scope.camera"] || false;
        }
        return false;
      } else {
        try {
          await this.requestCameraPermission();
          return true;
        } catch (err) {
          console.log("相机权限请求失败:", err);
          return false;
        }
      }
    } catch (err) {
      console.log("相机权限检查失败:", err);
      return false;
    }
  }
}

export default PermissionManager;
